﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CommonLib
{
    /// <summary>
    /// 自定义公共方法
    /// </summary>
    public class CommonMethod
    {
        /// <summary>
        /// 封装json格式数据，返回json字符串
        /// </summary>
        /// <param name="code">消息状态码</param>      
        /// <returns></returns>
        public static string FormatJsonObj(object code)
        {
            var data = new
            {
                code = code               
            };
            string jsonTxt = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            return jsonTxt;
        }
        /// <summary>
        /// 封装json格式数据，返回json字符串
        /// </summary>
        /// <param name="code">消息状态码</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static string FormatJsonObj(object code, object data)
        {
            var dataObj = new
            {
                code = code,
                data = data
            };
            string jsonTxt = Newtonsoft.Json.JsonConvert.SerializeObject(dataObj);
            return jsonTxt;
        }

        /// <summary>
        /// 清楚文本的html标签
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ClearHtml(string Content)
        {
            Content = Zxj_ReplaceHtml("&nbsp;", "", Content);
            Content = Zxj_ReplaceHtml("&#[^>]*;", "", Content);
            Content = Zxj_ReplaceHtml("</?marquee[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?object[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?param[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?embed[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?table[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml(" ", "", Content);
            Content = Zxj_ReplaceHtml("</?tr[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?th[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?p[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?a[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?img[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?tbody[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?li[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?span[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?div[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?th[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?td[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?script[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content);
            Content = Zxj_ReplaceHtml("on(mouse|exit|error|click|key)", "", Content);
            Content = Zxj_ReplaceHtml("<\\?xml[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("<\\/?[a-z]+:[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?font[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?b[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?u[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?i[^>]*>", "", Content);
            Content = Zxj_ReplaceHtml("</?strong[^>]*>", "", Content);
            return Content;
        }

        private static string Zxj_ReplaceHtml(string patrn, string strRep, string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                content = "";
            }
            return new Regex(patrn, RegexOptions.IgnoreCase).Replace(content, strRep);
        }

    }
}
